<?php
/**
 * XuYuan Project
 *
 * @package	XueYuan 
 * @author	Jiuchi Team
 * @since	Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Output class of API output
 * @author Wayne Yu
 */
class Apioutput {
	private	$CI;
	private $redis_host			=	'localhost';
	private $redis_port			=	6379;
	
	const STATUS_CODE_SUC = 200;
	const STATUS_CODE_ERR = 300;
	const STATUS_CODE_FAILAUTH = 400;
	const STATUS_CODE_REFUSE = 500;
	
	public function __construct($params = array()){
		//Set the super object
		$this->CI =& get_instance();
		
		//Set preferences for our token system
		foreach (array('redis_host', 'redis_port') as $key)
		{
			$this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
		}
		$this->redis = new Redis();
		$this->redis->connect($this->redis_host, $this->redis_port);
		$this->redis->setOption(Redis::OPT_PREFIX, 'api:');
	}
	
	/**
	 * Response success
	 * @param array $data
	 * @param string $msg
	 */
	public function apiResponseSuccess($data = array(),$msg = ''){
		$response = array(
				'Data'			=>	$data,
				'Msg'			=>	$msg,
				'StatusCode'		=>	$statusCode = self::STATUS_CODE_SUC
		);
		$this->_out($response);
	}

	/**
	 * Response error
	 * @param array $data
	 * @param string $msg
	 */
	public function apiResponseError($msg = ''){
		$response = array(
				'Msg'			=>	$msg,
				'StatusCode'	=>	self::STATUS_CODE_ERR
		);
		$this->_out($response);
	}
	
	/**
	 * Response authorization failed
	 * @param array $data
	 * @param string $msg
	 */
	public function apiResponseFailauth($msg = '验证失败，请重新登录！'){
		$response = array(
				'Msg'			=>	$msg,
				'StatusCode'	=>	self::STATUS_CODE_FAILAUTH
		);
		$this->_out($response);
	}
	
	/**
	 * Response refuse
	 * @param array $data
	 * @param string $msg
	 */
	public function apiResponseRefuse($msg = '非法请求！'){
		$response = array(
				'Msg'			=>	$msg,
				'StatusCode'	=>	self::STATUS_CODE_REFUSE
		);
		$this->_out($response);
	}
	
	/**
	 * Create the new auth code for user and return the times they try to get the auth code in 24h
	 *
	 * @param string $phone
	 * @param string $type
	 * @param string $code
	 * @return number
	 */
	public function setAuthCode($phone, $type, $code) {
		$this->redis->setex($phone.'_'.$type.'_code', 15, $code);
		$count = $this->redis->get($phone.'_count')?$this->redis->get($phone.'_count'):0;
		$count++;
		$this->redis->setex($phone.'_count', 60*60*24, $count);
		return $count;
	}
	
	/**
	 * Get the auth code we stored before
	 * @param string $phone
	 * @param string $type
	 * @return string
	 */
	public function getAuthCode($phone,$type) {
		$code	= $this->redis->get($phone.'_'.$type.'_code');
		return $code;
	}
	
	/**
	 * Delete the auth code we stored before
	 * @param string $phone
	 * @param string $type
	 * @return void
	 */
	public function delAuthCode($phone,$type) {
		$this->redis->delete($phone.'_'.$type.'_code');
		return $this;
	}
	
	/**
	 * Output the responce
	 * @param array $responce
	 */
	private function _out($responce){
		die(json_encode($responce));
	}
}